/**   
* @Package com.app.open.sys.role.service 
*/
package com.wlyy.bcwlw.sys.role.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.wlyy.bcwlw.common.cache.CacheEvict;
import com.wlyy.bcwlw.common.tree.BaseStaticTreeNode;
import com.wlyy.bcwlw.common.tree.StaticTree;
import com.wlyy.bcwlw.common.tree.TreeNode;
import com.wlyy.bcwlw.sys.application.dao.ApplicationDAO;
import com.wlyy.bcwlw.sys.application.entity.ApplicationDTO;
import com.wlyy.bcwlw.sys.config.ConfigConstant;
import com.wlyy.bcwlw.sys.permission.dao.PermissionDAO;
import com.wlyy.bcwlw.sys.permission.entity.PermissionDTO;
import com.wlyy.bcwlw.sys.role.dao.RoleDAO;
import com.wlyy.bcwlw.sys.role.dao.RolePermissionDAO;
import com.wlyy.bcwlw.sys.role.entity.RoleDTO;
import com.wlyy.bcwlw.sys.role.entity.RolePermissionDTO;
import com.wlyy.bcwlw.sys.user.dao.UserDAO;
import com.wlyy.bcwlw.sys.user.dao.UserRoleDAO;
import com.wlyy.bcwlw.sys.user.entity.UserDTO;
import com.wlyy.bcwlw.sys.user.entity.UserRoleDTO;

/**
 * @ClassName. RoleService
 * @Description. TODO
 * @author zhangyang
 * @date 2015年8月11日 下午3:27:16
 * @version V1.0
 */
@Transactional
@Service
public class RoleService {
	@Autowired
	private RolePermissionDAO rolePermissionDAO; 
	@Autowired
	private RoleDAO roleDAO;
	@Autowired
	private ApplicationDAO applicationDAO;
	@Autowired
	private UserRoleDAO userRoleDAO;
	@Autowired
	private PermissionDAO permissionDAO;
	@Autowired
	private UserDAO userDAO;
	
	
	public TreeNode getAllRoleTreeById(){
		List<BaseStaticTreeNode> nodeList = new ArrayList<BaseStaticTreeNode>();
		nodeList.add(new BaseStaticTreeNode(ConfigConstant.VirtualBaseTree, "角色树", null, "base"));
		List<ApplicationDTO> appList = applicationDAO.queryByObject(null);
		List<String> appIdList = new ArrayList<String>();
		if(appList !=null && appList.size() !=0){
			for(ApplicationDTO app:appList){
				nodeList.add(new BaseStaticTreeNode(app.getAppId(), app.getAppName(), ConfigConstant.VirtualBaseTree, "app"));
				appIdList.add(app.getAppId());
			}
		}
		
		List<RoleDTO> roleList = roleDAO.getRolesByAppId(appIdList);
		for(RoleDTO role:roleList){
			nodeList.add(new BaseStaticTreeNode(role.getId(),role.getName(), role.getAppId(), "role"));
		}
		
		StaticTree tree = new StaticTree(nodeList.toArray(new BaseStaticTreeNode[0]),ConfigConstant.VirtualBaseTree);
		tree.render();
		return tree.getRootNode();
	}
	
	@CacheEvict(key="userRoleCache")
	public RoleDTO save(RoleDTO roleDTO){
		if(StringUtils.isEmpty(roleDTO.getId())){
			roleDAO.create(roleDTO);
		}else{
			roleDAO.update(roleDTO);
		}
		return roleDTO;
	}
	public Map<String,Object> getRoleInfoById(String id){
		if(StringUtils.isEmpty(id)){
			return null;
		}
		Map<String,Object> retMap = new HashMap<String,Object>();
		RoleDTO roleDTO = roleDAO.findById(id);
		List<PermissionDTO> permissionList = permissionDAO.queryPermissionByRoleId(id);
		retMap.put("roleDTO", roleDTO);
		retMap.put("permissionList", permissionList);
		return retMap;
	} 
	
	@CacheEvict(key={"userRoleCache","userPermissionCache"})
	public void savePermissionRelation(String roleId,String[] addPermissionIds,String[] delPermissionIds){
		if(addPermissionIds != null && addPermissionIds.length !=0){
			for(String addId:addPermissionIds){
				RolePermissionDTO queryDTO = new RolePermissionDTO();
				queryDTO.setPermissionId(addId);
				queryDTO.setRoleId(roleId);
				int count = rolePermissionDAO.count(queryDTO);
				if(count >0){
					continue;
				}
				rolePermissionDAO.create(queryDTO);
			}
		}
		if(delPermissionIds != null && delPermissionIds.length !=0){
			for(String delId:delPermissionIds){
				RolePermissionDTO rolePermissionDTO = new RolePermissionDTO();
				rolePermissionDTO.setPermissionId(delId);
				rolePermissionDTO.setRoleId(roleId);
				rolePermissionDAO.remove(rolePermissionDTO);
			}
		}
	}
	
	@CacheEvict(key="userRoleCache")
	public boolean removeRoleById(String roleId){
		if(StringUtils.isEmpty(roleId)){
			return false;
		}
		RoleDTO queryRole = roleDAO.findById(roleId);
		if(queryRole == null){
			return false;
		}
		//删除角色-权限
		RolePermissionDTO rolePermissionDTO = new RolePermissionDTO();
		rolePermissionDTO.setRoleId(roleId);
		rolePermissionDAO.remove(rolePermissionDTO);
		//删除角色-用户
		UserRoleDTO userRoleDTO = new UserRoleDTO();
		userRoleDTO.setRoleId(roleId);
		userRoleDAO.remove(userRoleDTO);
		//删除角色
		roleDAO.removeById(roleId);
		return true;
	}
	
	@CacheEvict(key="userRoleCache")
	public boolean deleteUserWithRoleRelation(String roleId,String userId){
		if(StringUtils.isEmpty(roleId) || StringUtils.isEmpty(userId)){
			return false;
		}
		UserRoleDTO  userRoleDTO = new UserRoleDTO();
		userRoleDTO.setRoleId(roleId);
		userRoleDTO.setUserId(userId);
		int count =userRoleDAO.remove(userRoleDTO);
		if(count == 0){
			return false;
		}
		return true;
	}
	
	@CacheEvict(key="userRoleCache")
	public boolean addUserWithRoleRelation(String roleId,String userName){
		if(StringUtils.isEmpty(roleId) || StringUtils.isEmpty(userName)){
			return false;
		}
		UserDTO userDTO = userDAO.findByUserName(userName);
		if(userDTO == null){
			return false;
		}
		UserRoleDTO  userRoleDTO = new UserRoleDTO();
		userRoleDTO.setRoleId(roleId);
		
		userRoleDTO.setUserId(userDTO.getUserId());
		int count = userRoleDAO.count(userRoleDTO);
		if(count !=0){
			return false;
		}
		userRoleDAO.create(userRoleDTO);
		return true;
	}
}
